package com.china08.yxyapi.api.manage.tec.office.meetingv1_1;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.meeting.VideoMeeting;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.VideoMeetingRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.DateUtils;

/**
 * 获取当前账户下的教育局所发布的视频会议
 *
 */
@RestController("tea_meeting_page")
public class VideoMeetingv1_1PageController extends YxyApiBaseController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private VideoMeetingRepository videoMeetingRepository;

    @RequestMapping(value = "/api_yw/manage/tea/office/meetingv1_1/videomeet/page", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {

        YxyProfile yxyProfile = getProfile(request, response);
        String schooId = yxyProfile.getSchoolId();
        String userId = yxyProfile.getId();

        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");

        StringBuffer sqlBuffer = new StringBuffer();
        StringBuffer sqlCountBuffer = new StringBuffer();
        StringBuffer sqlListBuffer = new StringBuffer();
        List<Object> objects = new ArrayList<Object>();

        sqlBuffer.append(" FROM yxy_yxy.yxy_video_meeting m ");
        sqlBuffer.append(" LEFT JOIN yxy_yxy.yxy_video_meeting_user u ");
        sqlBuffer.append(" ON m.id = u.meeting_id  ");
        sqlBuffer.append(" LEFT JOIN yxy_yxy.yxy_video_meeting_invite_user i ");
        sqlBuffer.append(" ON m.id = i.meeting_id  ");
        sqlBuffer.append(" WHERE (( m.school_id = ? AND m.speaker_id = ? ) ");
        sqlBuffer.append(" OR ( m.school_id = ? AND u.user_id IN ( ?,? ) ) ");
        sqlBuffer.append(" OR ( i.school_id = ? AND i.user_id = ? )) ");

        if (StringUtils.isNotBlank(reqModel.title)) {
            sqlBuffer.append(" AND m.title LIKE ? ");
        }

        if (StringUtils.isNotBlank(reqModel.status)) {
            sqlBuffer.append(" AND m.status = ? ");
        }

        // 数量
        sqlCountBuffer.append(" SELECT count(DISTINCT m.id) ");
        sqlCountBuffer.append(sqlBuffer);

        // id集合
        int limitCount = pageable.getPageNumber() * pageable.getPageSize();
        sqlListBuffer.append(" SELECT DISTINCT m.id ");
        sqlListBuffer.append(sqlBuffer);
        sqlListBuffer.append(" ORDER BY m.created_date DESC ");
        sqlListBuffer.append(" LIMIT " + limitCount + "," + pageable.getPageSize());

        objects.add(schooId);
        objects.add(userId);
        objects.add(schooId);
        objects.add(userId);
        objects.add(schooId);
        objects.add(schooId);
        objects.add(userId);

        if (StringUtils.isNotBlank(reqModel.title)) {
            objects.add("%" + reqModel.title + "%");
        }

        if (StringUtils.isNotBlank(reqModel.status)) {
            objects.add(reqModel.status);
        }

        Integer listCount = jdbcTemplate.queryForObject(sqlCountBuffer.toString(), objects.toArray(), Integer.class);
        List<String> resultLst = jdbcTemplate.queryForList(sqlListBuffer.toString(), objects.toArray(), String.class);

        List<VideoMeeting> meetLst = videoMeetingRepository.findByIdInOrderByCreatedDateDesc(resultLst);

        List<RespModel> list = new ArrayList<VideoMeetingv1_1PageController.RespModel>();
        for (VideoMeeting vm : meetLst) {
            RespModel model = new RespModel();
            model.id = vm.getId();
            model.createdDate = DateUtils.formatDate2(vm.getCreatedDate());
            model.createdByName = vm.getCreatedByName();
            model.title = vm.getTitle();
            String date = DateUtils.formatDate2(vm.getStartDate());
            String start = DateUtils.formatDate11(vm.getStartDate());
            String end = DateUtils.formatDate11(vm.getEndDate());
            model.date = date + " " + start + "-" + end;
            model.speakerName = vm.getSpeakerName();
            long difference = DateUtils.difference2(vm.getStartDate(), new Date());
            if ((difference <= 30 && vm.getStatus() == 0) || vm.getStatus() == 1) {
                model.getInto = 1;
            } else {
                model.getInto = 0;
            }
            model.status = vm.getStatus();
            list.add(model);
        }

        Page<RespModel> page = new PageImpl<RespModel>(list, pageable, listCount);
        return page;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiPageReqModel {
        // 会议状态
        public String status;
        // 输入框
        public String title;
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {
        // id
        public String id;
        // 发布时间
        public String createdDate;
        // 发布人
        public String createdByName;
        // 会议主题
        public String title;
        // 会议时间
        public String date;
        // 主讲人
        public String speakerName;
        // 状态
        public Integer status;
        // 能否进入会议 0-不能，1-能
        public Integer getInto;
    }

}
